package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**权限控制层
 * @author YH
 * @date 2020/4/17 20:58
 */
@RestController
@RequestMapping(value = "/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    //查询所有权限列表
    @RequestMapping(value = "/findPermissionByAll")
    public Result findPermissionByAll(){
        try {
            List<Map<String, Object>> list = permissionService.findPermissionByAll();
            return new Result(true,MessageConstant.QUERY_ALL_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ALL_PERMISSION_FAIL);
        }
    }


    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public Result findAll(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult result = permissionService.findAll(queryPageBean);
            return new Result(true,MessageConstant.QUERY_ALL_PERMISSION_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ALL_PERMISSION_FAIL);
        }
    }

    /**
     * 新增权限
     * Integer[] key：springmvc框架自动转换
     *
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }


    /**
     编辑权限
     */


    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /**
     * 删除角色的权限
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer roleId, Integer[] permissionIds) {
        try {
            permissionService.deleteById(roleId,permissionIds);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 删除单个权限
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            permissionService.delete(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 根据角色id查询权限
     */
    @RequestMapping("/findById")
    public Result findById(Integer roleId) {
        try {
            permissionService.findById(roleId);
            return new Result(true, MessageConstant.QUERY_ALL_PERMISSION_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ALL_PERMISSION_FAIL);
        }
    }
    /**
     * 根据权限id查询权限
     */
    @RequestMapping("/QueryById")
    public Result QueryById(Integer id) {
        try {
            Map map = permissionService.QueryById(id);
            return new Result(true, MessageConstant.QUERY_ALL_PERMISSION_SUCCESS, map);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ALL_PERMISSION_FAIL);
        }
    }
}
